<template>
  <!-- 文件管理新增按钮弹窗 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="defColumns"
      :scroll="tableScroll"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
      >
    
    
    
    </p-table>
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="12">
            <p-form-item label="领用人员工号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <p-input readonly placeholder="领用人员工号" :defaultValue="EnhanceJS.userName"  v-model="EnhanceJS.labelTemplateNo"></p-input> -->
              <p-input-search
                class="hideName"
                :defaultValue="userInfo.uid"
                v-model="userInfo.uid"
                placeholder="领用人员工号"
                readOnly
                required="true"
                @search="onSearchDepUser">
                <p-button slot="enterButton" :disabled="disabled">选择用户</p-button>
              </p-input-search>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="领用人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input readonly v-model="userInfo.name" placeholder="领用人姓名"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="领用人联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input readonly v-model="userInfo.mobile" placeholder="领用人联系方式"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="领用部门 " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input readonly v-model="userInfo.departmentName" placeholder="领用部门 "></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="领用时间 " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-group compact >
                <p-date-picker style="width: 50%" :defaultValue="Data"  showTime="true" required="true"
                />
              </p-input-group>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
    <j-select-user-by-dep-modal
      ref="selectModal"
      :modal-width="1250"
      :multi="true"
      @ok="selectOK"
      @getName="getName"
      :user-ids="value"
      @initComp="initComp"/>
  </p-modal>

  
</template>

<script>
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import { httpAction, putAction, api,postActionBasic} from "@/api/manage";
import pick from "lodash.pick";
import JUpload from "@/components/jeecg/JUpload";
import JDictSelectTag from "@/components/dict/JDictSelectTag";
import JSelectUserByDepModal from './JSelectUserByDepModal'
export default {
  name: "MesQcQualityFileManagementModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JUpload,
    JDictSelectTag,
    JSelectUserByDepModal
  },
  props: {
    EnhanceJS: {
      default: "",
      required: false,
    },
  },
  watch:{
    EnhanceJS(){
      console.log(this.EnhanceJS.dataSource)
      this.userInfo=this.EnhanceJS.userInfo
      this.Data=this.EnhanceJS.Data
      this.dataSource=this.EnhanceJS.dataSource
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "基础信息",
      width: 1200,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      
      isQuery:false,
      disabled:false,
      confirmLoading: false,
      userInfo:{},
      Data:'',
      validatorRules: {
        labelTemplateNo: {
          rules: [{ required: true, message: "请输入模板编码!" }],
        },
        labelTemplateName: {
          rules: [{ required: true, message: "请输入模板名称!" }],
        },
        labelTemplateDescrtion: {
          rules: [{ required: true, message: "请输入模板描述" }],
        },
        labelTemplateLength: {
          rules: [{ required: true, message: "请输入模板尺寸" }],
        },
        dptname: { rules: [{ required: true, message: "责任部门" }] },
        labelColor: { rules: [{ required: true, message: "标签纸颜色!" }] },
        ribbonsType: { rules: [{ required: true, message: "碳带型号!" }] },
        printerType: { rules: [{ required: true, message: "打印机型号!" }] },
        fileType: { rules: [{ required: true, message: "打印份数!" }] },
      },
      defColumns: [
        {
          title: "序号",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
       {
          title: "工单号/条码生成编码",
          align: "center",
          dataIndex: "productionno",
          customRender: function (t, r, index) {
           if(t){
             return t
           }else{
             return r.barcodeProduceNo
           }
          },
        },
        {
          title: "首标签条码",
          align: "center",
          dataIndex: "beginBarcode",
        },
        {
          title: "尾标签条码",
          align: "center",
          dataIndex: "endBarcode",
        },
        
        {
          title: "标签模板名称",
          align: "center",
          dataIndex: "labelTemplateName",
        },

        
        {
          title: "条码规则名称",
          align: "center",
          dataIndex: "ruleName",
        },

        {
          title: "数量",
          align: "center",
          dataIndex: "qty",
        },
      ],
      url: {
        add: "/mes-code/mesBcUser",
        edit: "/mes-quality/mesQcQualityFileManagement/edit", 
      },
    };
  },
  mounted() {
  },
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [{ ruleCode: "mes_quality_file_rule", columnName: "fileCode" }],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
        }
      });
    },
     
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "scientificPlease",
            "fileCode",
            "fileName",
            "fileVersion",
            "fileUrl",
            "fileType",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "remarks",
            "status"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
     getName (val) {
        this.showName = val.toString()
      },
      initComp (userNames) {
        this.userNames = userNames
      },
      onSearchDepUser () {
        this.$refs.selectModal.showModal()
      },
      selectOK (rows, idstr) {
        this.userInfo=rows
      },
    handleOk() {
      console.log(this.EnhanceJS.userInfo)
      const that = this;
      // 触发表单验证
      this.confirmLoading= true;
      let httpurl = "/mes-code/mesBcPrint";
          let method = "post";
          let arr=[]
          let formData
          this.dataSource.forEach(item => {
            formData = {
            id:item.id,
            empno:this.userInfo.uid,
            empname:this.userInfo.name,
            linkphone :this.userInfo.mobile,
            dptname:this.userInfo.departmentName,
            status:1,
          }
          arr.push(formData)
          });
          if(arr.length<=0){
            alert('请选择工单号/条码生成编码')
          }else{
            
          putAction(httpurl,arr).then((res)=>{
            if(res.code==0){
              this.$message.info('领用成功');
              this.confirmLoading=false
              this.visible=false
              this.$emit('passVaule')
            }else{
               this.$message.info(res.msg);
              }
            })
}

    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "scientificPlease",
          "fileCode",
          "fileName",
          "fileVersion",
          "fileUrl",
          "fileType",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "remarks",
          "status"
        )
      );
    },
  },
};
</script>
